/*
 * @(#)Bulk.java   1.0   May 18, 2007
 *
 * Copyright 2000-2007 ETH Zurich. All Rights Reserved.
 *
 * This software is the proprietary information of ETH Zurich.
 * Use is subject to license terms.
 *
 * @(#) $Id: Bulk.java 433 2007-05-22 12:45:58Z D\aldespin $
 */
package ch.ethz.globis.javaom.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import ch.ethz.globis.javaom.system.Metadata;

/**
 * @author aldespin
 * @version 1.0
 */
public class Bulk extends Object implements Collection<Object> {

   private java.util.Collection<Object> value;

   public Bulk(final Metadata metadata) {
      super(metadata);
      this.value = new ArrayList<Object>();
   }

   public Bulk() {
      super();
      this.value = new ArrayList<Object>();
      // TODO dress with object and bulk
      // this.dress(Metadata.METADATA.getTypeObject(Metadata.OBJECT));
      // this.dress(Metadata.METADATA.getTypeObject(Metadata.BULK));
   }

   /**
    * {@inheritDoc}
    */
   public boolean add(Object arg0) {
      return this.value.add(arg0);
   }

   /**
    * {@inheritDoc}
    */
   public boolean addAll(Collection< ? extends Object> arg0) {
      return this.value.addAll(arg0);
   }

   /**
    * {@inheritDoc}
    */
   public void clear() {
      this.value.clear();
   }

   /**
    * {@inheritDoc}
    */
   public boolean contains(java.lang.Object arg0) {
      return this.value.contains(arg0);
   }

   /**
    * {@inheritDoc}
    */
   public boolean containsAll(Collection< ? > arg0) {
      return this.value.containsAll(arg0);
   }

   /**
    * {@inheritDoc}
    */
   public boolean isEmpty() {
      return this.value.isEmpty();
   }

   /**
    * {@inheritDoc}
    */
   public Iterator<Object> iterator() {
      return this.value.iterator();
   }

   /**
    * {@inheritDoc}
    */
   public boolean remove(java.lang.Object arg0) {
      return this.value.remove(arg0);
   }

   /**
    * {@inheritDoc}
    */
   public boolean removeAll(Collection< ? > arg0) {
      return this.removeAll(arg0);
   }

   /**
    * {@inheritDoc}
    */
   public boolean retainAll(Collection< ? > arg0) {
      return this.value.retainAll(arg0);
   }

   /**
    * {@inheritDoc}
    */
   public int size() {
      return this.value.size();
   }

   /**
    * {@inheritDoc}
    */
   public java.lang.Object[] toArray() {
      return this.value.toArray();
   }

   /**
    * {@inheritDoc}
    */
   public <T> T[] toArray(T[] arg0) {
      return this.value.toArray(arg0);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public String toString() {
      final StringBuffer result = new StringBuffer();
      final String thisKey = Metadata.METADATA.getObjectKey(this);
      if (thisKey != null) {
         result.append("<" + thisKey + " id=\"" + this.getID() + "\" size=\"" + this.size()
               + "\">");
      } else {
         result.append("<bulk id=\"" + this.getID() + "\">");
      }
      for (Object current : this.value) {
         if (current instanceof Tuple || current instanceof AttributeValue) {
            result.append(current.toString());
         } else {
            final String key = Metadata.METADATA.getObjectKey(current);
            if (key != null) {
               result.append("<" + key + " id=\"" + current.getID() + "\"/>");
            } else {
               result.append("<object id=\"" + current.getID() + "\"/>");
            }
         }
      }
      if (thisKey != null) {
         result.append("</" + thisKey + ">");
      } else {
         result.append("</bulk>");
      }
      return result.toString();
   }
}
